package com.example.demo.dao;

import com.example.demo.entity.AdminVo;
import com.example.demo.entity.UserVo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Admindao {
    public AdminVo findByName(String username) throws Exception{
        AdminVo adminVo=null;
        Connection con= null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            Class.forName(DB.JDBC_DRIVER);
            con= DriverManager.getConnection(DB.JDBC_URL,DB.JDBC_USER,DB.JDBC_PASSWORD);
            ps=con.prepareStatement("select * from Admin where Username=? ");//UserID与数据库内的UserID对应，要一致
            ps.setString(1,username);//userID是变量名
            rs=ps.executeQuery();
            if (rs.next()){
                adminVo=new AdminVo();
                adminVo.setAdminId(rs.getInt("AdminID"));
                adminVo.setUsername(rs.getString("Username"));
                adminVo.setPassword(rs.getString("Password"));
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            throw new Exception("数据库异常:"+e.getMessage());
        }finally {
            if(rs!=null) rs.close();
            if(ps!=null) ps.close();
            if(con!=null) con.close();
        }
        return adminVo;
    }
}
